//
//  MBGroupOrderTypeView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGroupOrderTypeView.h"

@interface MBGroupOrderTypeView ()
@property (nonatomic,weak)UIButton* selectButton;

@end

@implementation MBGroupOrderTypeView

- (instancetype)initWithFrame:(CGRect)frame andType:(NSArray*)typeArray  andFont:(UIFont*)font
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        for (int i=0; i<typeArray.count; i++) {
            UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height/typeArray.count*i, self.width, self.height/typeArray.count)];
            button.tag = i+1;
            [button addTarget:self action:@selector(handleClickTypeButton:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:typeArray[i] forState:UIControlStateNormal];
            [button setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
            [button setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
            button.titleLabel.font = font;
            [self addSubview:button];
            
            UIView* line = [[UIView alloc]init];
            line.backgroundColor = [UIColor colorWithR:236 G:236 B:236];
            [self addSubview:line];
            
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(button);
                make.height.mas_equalTo([UIView lf_sizeFromIphone6:1]);
            }];
            if (i == 0) {
                self.selectButton = button;
                button.selected = YES;
            }
        }
    }
    return self;
}

-(void)handleClickTypeButton:(UIButton*)button
{
    if (self.selectButton.tag != button.tag) {
        self.typeName = button.titleLabel.text;
        self.selectButton.selected = NO;
        button.selected = YES;
        self.selectButton = button;
        self.groupType = (int)button.tag;
    }
}

@end
